package com.example.jsf.config;//package com.example.security.config;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.stereotype.Component;
//
//import java.io.PrintWriter;
//
//@Component
//public class SecurityConfig extends WebSecurityConfigurerAdapter {
//
//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.inMemoryAuthentication()
//                .passwordEncoder(new BCryptPasswordEncoder())//在此处应用自定义PasswordEncoder
//                .withUser("user")
//                .password(new BCryptPasswordEncoder().encode("123"))
//                .roles("USER");
//    }
//
//    @Override
//    protected void configure(HttpSecurity http) throws Exception {
//        http
//                .authorizeRequests()
//                .anyRequest()
//                .authenticated()
//                .and()
//                .formLogin()
//                .usernameParameter("username")
//                .passwordParameter("password")
//                .successHandler((httpServletRequest, httpServletResponse, authentication) -> {
//                    httpServletResponse.setContentType("application/json;charset=utf-8");
//                    PrintWriter writer = httpServletResponse.getWriter();
//                    writer.write(new ObjectMapper().writeValueAsString("登录成功!"));
//                    writer.flush();
//                    writer.close();
//                })
//                .failureHandler((httpServletRequest, httpServletResponse, e) -> {
//                    httpServletResponse.setContentType("application/json;charset=utf-8");
//                    PrintWriter writer = httpServletResponse.getWriter();
//                    writer.write(new ObjectMapper().writeValueAsString("登录失败!"));
//                    writer.flush();
//                    writer.close();
//                })
//                .permitAll()
//                .and()
//                .exceptionHandling()
//                .authenticationEntryPoint((httpServletRequest, httpServletResponse, e) -> {
//                    httpServletResponse.setContentType("application/json;charset=utf-8");
//                    PrintWriter writer = httpServletResponse.getWriter();
//                    writer.write(new ObjectMapper().writeValueAsString("未登录!"));
//                    writer.flush();
//                    writer.close();
//                })
//                .and()
//                .csrf()
//                .disable();
//
//    }
//}
